package com.skd.mall.product.service.impl;

import com.mysql.cj.util.StringUtils;
import com.skd.mall.product.entity.AttrAttrgroupRelationEntity;
import com.skd.mall.product.entity.AttrGroupEntity;
import com.skd.mall.product.entity.SkuInfoEntity;
import com.skd.mall.product.service.AttrAttrgroupRelationService;
import com.skd.mall.product.service.AttrGroupService;
import com.skd.mall.product.service.SkuInfoService;
import com.skd.mall.product.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.skd.common.utils.PageUtils;
import com.skd.common.utils.Query;

import com.skd.mall.product.dao.ProductAttrValueDao;
import com.skd.mall.product.entity.ProductAttrValueEntity;
import com.skd.mall.product.service.ProductAttrValueService;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    @Autowired
    private SkuInfoService skuInfoService;

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    private ProductAttrValueDao productAttrValueDao;

    @Override
    public List<ItemVO.SpuItemGroupAttrVo> getproductAttrValueBySkuId(Long skuId) {
        //查询sku对应的spuID
        SkuInfoEntity sku = skuInfoService.getSkuBySkuId(skuId);
        //catelogID
        Long catelogId = sku.getCatalogId();
        //spuID
        Long spuId = sku.getSpuId();
        //获取attrGroup
        List<AttrGroupEntity> attrGroupEntities = attrGroupService.list(
                new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId)
        );
        List<ItemVO.SpuItemGroupAttrVo> spuItemGroupAttrVos = attrGroupEntities.stream().map(item -> {
            ItemVO.SpuItemGroupAttrVo spuItemGroupAttrVo = new ItemVO.SpuItemGroupAttrVo();

            spuItemGroupAttrVo.setGroupName(item.getAttrGroupName());
            //获取attr
            List<AttrAttrgroupRelationEntity> relations = attrAttrgroupRelationService.list(
                    new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", item.getAttrGroupId()));

            List<ItemVO.SpuBaseAttrVo> spuBaseAttrVos = relations.stream().map(relation -> {
                ItemVO.SpuBaseAttrVo spuBaseAttrVo = new ItemVO.SpuBaseAttrVo();
                ProductAttrValueEntity productAttrValue = productAttrValueDao.selectOne(
                        new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId).eq("attr_id", relation.getAttrId())
                );
                if(productAttrValue!=null){
                    spuBaseAttrVo.setAttrName(productAttrValue.getAttrName());
                    spuBaseAttrVo.setAttrValue(productAttrValue.getAttrValue());
                }
                return spuBaseAttrVo;
            }).collect(Collectors.toList());

            spuItemGroupAttrVo.setBaseAttrs(spuBaseAttrVos);
            return spuItemGroupAttrVo;
        }).collect(Collectors.toList());

        return spuItemGroupAttrVos;
    }

}